#!/bin/bash

# Copyright (c) Los Alamos National Security, LLC, and others.

# Deficiencies. This script does not install:
#
# 1. The geographic libraries. I tried at length to get things to build
#    properly -- the issue is that everything seems to work, but then key
#    attributes of the gdal module are missing -- but was unable to resolve
#    the problem. Because we are not actively using the geo stuff at the
#    moment, this is acceptable.
#
# 2. cmdtest. This is because cmdtest and its dependencies (python-cliapp and
#    python-ttystatus) are Python 2-only, the cmdtest script itself has a
#    hardcoded /usr/bin/pythonm and I can't figure out how to get them
#    installed in the Python 3 environment with a reasonable amount of
#    screwing around. So, there are no cmdtests in this installation.
#
# Source code is in deps.tar.gz in case you are smarter than me. In this case,
# please send a patch!

set -e

QUACBASE=$(cd $(dirname $0)/.. && pwd)
PREFIX=$QUACBASE/deps
SRC=$PREFIX/src
ENV=$QUACBASE/env
MAKEJ=${3:1}
PIPINSTALL="pip install --index-url=file://$QUACBASE/deps/packages/simple"

# strip out geo libraries
egrep -v '^(Django|pyproj)==' requirements.txt > requirements-nogeo.txt


function install_ {

    # this links the binaries to my libraries without needing LD_LIBRARY_PATH
    export LDFLAGS=-Wl,-rpath=$PREFIX/lib

    # install Python and other non-pip stuff
    pushd $SRC
    install_python
    . $ENV/bin/activate
    export PATH=$PREFIX/bin:$PATH
    PYTHON=$(command -v python)
    if [[ $PYTHON != $ENV/bin/python ]]; then
        echo
        echo "wrong Python: $PYTHON, need $ENV/bin/python"
        exit 1
    fi
    install_apsw
    #install_swig
    #install_proj
    #install_geos
    #install_gdal
    popd

    unset LDFLAGS  # breaks numpy build
    #$PIPINSTALL GDAL
    $PIPINSTALL numpy  # otherwise scipy et al. barf later
    $PIPINSTALL pbr    # lockfile can't bring it in automatically
    $PIPINSTALL -r requirements-nogeo.txt
    pip freeze --local
}

function make_ {
    make -j$MAKEJ "$@"
    make install
    make clean
    popd
}

function install_apsw {
    unzip -o apsw-*.zip
    tar xf sqlite-autoconf-*.tar.gz
    pushd apsw-*
    rm -f sqlite3
    ln -s ../sqlite-autoconf-*[0-9] sqlite3
    python setup.py build --enable-all-extensions install
    popd
}

function install_gdal {
    tar xf gdal-*.tar.gz
    pushd gdal-*
    ./configure --with-static-proj4=$PREFIX \
                --with-python=$QUACBASE/env/bin/python \
                --prefix=$PREFIX
    make_
}

function install_geos {
    tar xjf geos-*.tar.bz2
    pushd geos-*
    ./configure --prefix=$PREFIX
    make_
}

function install_proj {
    tar xf proj-*.tar.gz
    pushd proj-*
    ./configure --without-jni --prefix=$PREFIX
    make_
}

function install_python {
    tar xf Python-*.tgz
    pushd Python-*
    ./configure --enable-shared --prefix=$PREFIX
    make_
    $PREFIX/bin/pyvenv $ENV
}

function install_swig {
    tar xf swig-*.tar.gz
    pushd swig-*
    ./configure --prefix=$PREFIX
    make_
}

install_
